package cz.muni.fi.pa165.soccergamesarchive.rest.services;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.PlayerDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.PlayerServiceBeanLocal;
import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Rest Service for Player
 *
 * @author Tomas Plevko
 */
@Stateless
@Path(value = "player")
@DeclareRoles("Archiver")
public class PlayerRestService {

    @EJB
    private PlayerServiceBeanLocal playerService;

    public PlayerRestService() {
    }

    @POST
    @Path("new")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public PlayerDTO addPlayer(PlayerDTO player) throws RuntimeException {

        return playerService.addPlayer(player);
    }

    @POST
    @Path("change")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public PlayerDTO changePlayer(PlayerDTO player) throws RuntimeException {

        return playerService.changePlayer(player);
    }

    @POST
    @Path("delete")
    @Consumes(MediaType.TEXT_XML)
    @RolesAllowed("Archiver")
    public void removePlayer(PlayerDTO player) throws RuntimeException {

        playerService.removePlayer(player);
    }

    @GET
    @Path("getall")
    @Produces(MediaType.TEXT_XML)
    public List<PlayerDTO> getAllPlayers() throws RuntimeException {

        return playerService.getAllPlayers();
    }

    @GET
    @Path("getlimnumber/{offset}/{limit}")
    @Produces(MediaType.TEXT_XML)
    @Consumes(MediaType.TEXT_XML)
    public List<PlayerDTO> getLimitedNumberOfPlayers(
            @PathParam("offset") int offset, @PathParam("limit") int limit)
            throws RuntimeException {

        return playerService
                .getLimitedNumberOfPlayers(offset, limit, null, null);
    }

    @GET
    @Path("id/{id}")
    @Produces(MediaType.TEXT_XML)
    public PlayerDTO getPlayerById(@PathParam("id") Long id) throws
            RuntimeException {

        return playerService.getPlayerById(id);
    }

    @GET
    @Path("count")
    @Consumes(MediaType.TEXT_XML)
    @Produces(MediaType.TEXT_PLAIN)
    public int getFilteredPlayersCount() throws RuntimeException {

        return playerService.getFilteredPlayersCount(null);
    }

    @GET
    @Path("find/{firstName}/{lastame}")
    @Produces(MediaType.TEXT_XML)
    public List<PlayerDTO> findPlayer(@PathParam("firstName") String firstName,
                                      @PathParam("lastName") String lastName)
            throws RuntimeException {

        return playerService.findPlayer(firstName, lastName);
    }

}
